/**
 * @file main.c
 * @author Linquan  
 * @brief 
 * @version 0.1
 * @date 2024-04-28
 * 
 * @copyright Copyright (c) 2024
 * 
 */
#include <stdio.h>
#include <stdlib.h>

#define DEBUG_PRINT 0

unsigned char key[8] = 
{
    0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};


static void Printf_data(unsigned char *data_point)
{
#if DEBUG_PRINT 
    for(int i = 0; i< 32; i++)
    {
        printf("%#X, ",*data_point);
        data_point++;

    }
#endif
}

static unsigned char check_16x16_hv_lv(unsigned char c_hv, \
                                    unsigned char c_lv,\
                                    char * file, int line)
{
    unsigned short font_n = (c_hv<<8)+c_lv;

    if(font_n < 0XA1A1 || font_n > 0XFEFE)
    {
        printf("error: the para is overflow ! \n file name: %s\n code line:%d\n",file,line);
        return 1;
    }
    return 0;
}

/**
 * @brief 从指定的字库文件中读出一个字符的字模数据
 * 
 */
void get_one_font_16x16_from_file( char *fname, \
                            unsigned char c_hv,\
                            unsigned char c_lv, \
                            unsigned char *read_buff )
{  
    unsigned char print_buff[16][16];
    unsigned char hv_lv[2]={c_hv, c_lv};
    
    if(check_16x16_hv_lv(c_hv, c_lv,__FILE__, __LINE__)) return;
    
    /*打开文件*/
    FILE *HZK;
    int errnum = 0;
    if((HZK = fopen(fname, "rb")) == NULL) 
    {
       perror("Can't open font file!");
    }

    /*计算偏移量*/
    int hv = *hv_lv - 0xa1;
    int lv = *(hv_lv+1) - 0xa1;
    long offset = (long) (94 * hv + lv) * 32;

    /*读取数据*/
    fseek(HZK, offset, SEEK_SET);
    fread(read_buff, 32, 1, HZK);
    fclose(HZK);

    /*打印读出数据*/
    Printf_data(read_buff);

    /*打印字模*/
    #if DEBUG_PRINT
    printf("\n ");
    for (int kx = 0; kx < 16; kx++)//16行
    {
        for (int jx = 0; jx < 2; jx++)//每一行有两个字节
        {
            for (int ix = 0; ix < 8; ix++)//每个字节有8位
            {
                print_buff[kx][(ix+(jx*8))] = (read_buff[kx * 2 + jx] & key[ix]) ? 1:0;
                printf("%s", print_buff[kx][ix +(jx*8)]? "#":"," );

            }
        }
        printf("\n");
    }
    # endif
}


/**
 * @brief 转化为列行式，低位在下
 * 
 * @param read_buff 传入的从HZK16中读出的数据
 * @param trans_buff 转化结果
 */
void data_trans(unsigned char *read_buff, unsigned char *trans_buff )
{
    unsigned char print_buff[16][16];

    /*字模转换为像素点*/ 
    for (int kx = 0; kx < 16; kx++)//16行
    {
        for (int jx = 0; jx < 2; jx++)//每一行有两个字节
        {
            for (int ix = 0; ix < 8; ix++)//每个字节有8位
            {
                print_buff[kx][(ix+(jx*8))] = (read_buff[kx * 2 + jx] & key[ix]) ? 1:0;
               //printf("%s", print_buff[kx][ix +(jx*8)]? "1":"0" );//用 0 ，1 表示像素点
            }
        }
        //printf("\n");
    }
    

    /*重组字模*/
    /*1, 上半部分转化*/
    for(int num = 0; num < 16; num++)//16列
    {
        for(int bits_num = 7; bits_num >0; bits_num--)//一个字节
        {
            // 列不变，变行
            trans_buff[num] |= (print_buff[bits_num][num] <<(7-bits_num));
        }
    }
    /*2, 下半部分转化*/
    for(int num = 16; num < 32; num++)//16列
    {
        for(int bits_num = 7; bits_num >0; bits_num--)//一个字节
        {
            // 列不变，变行
            trans_buff[num] |= (print_buff[bits_num +8][num-16] <<(7-bits_num));
        }
    }

    /*打印重组后的字模数据*/
    Printf_data(trans_buff);
    
}


/**
 * @brief 生成 一个数组数据,可重定向到C文件
 * 
 */

void PrintFont_ArryForamt( unsigned char c_hv,\
                                    unsigned char c_lv, \
                                    unsigned char *trans_buff)
                    
{
    unsigned short cc = (c_hv<<8)+ (c_lv);

    if(check_16x16_hv_lv(c_hv, c_lv, __FILE__, __LINE__)) return;

    for(int i=0; i<32; i++)
    {
        if(i == 0)
        {
            printf("   { 0X%04X, { 0X%02x,", cc ,trans_buff[i] );
        }
        else if(i == 16)
        {
                    printf("\n             0X%02x,",trans_buff[i]);
        }
        else if (i == 31)
        {          
            printf("0X%02x}},//%c%c\n",trans_buff[i], c_hv,c_lv);
        }     
        else
        {
            printf("0X%02x,",trans_buff[i]);
        }
    }
}

/**
 * @brief   将数据写入文件中，不好使，只能写入一条数据
 * 
 */
void  save_data_to_file( unsigned char c_hv,\
                         unsigned char c_lv, \
                         unsigned char *trans_buff )
{
    FILE *HZKb;
    if((HZKb = fopen("../../../../hzk1", "ab+")) == NULL) {
        printf("Can't open font file!");
    }
    int hv = c_hv - 0xa1;
    int lv = c_lv - 0xa1;
    long offset = (long) (94 * hv + lv) * 32;
    fseek(HZKb, offset, SEEK_SET);
    fwrite(trans_buff, 32, 1, HZKb);
    fclose(HZKb);
}

/**
 * @brief  读取保存到文件中的转换后的数据
 * 
 */
void trans_get_font_from_file( unsigned char c_hv,\
                         unsigned char c_lv, \
                         unsigned char *read_buff )
{  
    unsigned char print_buff[16][16];
    unsigned char hv_lv[2]={c_hv, c_lv};

    /*打开文件*/
    FILE *HZK;
    int errnum = 0;
    if((HZK = fopen("../../../../hzk1", "rb")) == NULL) {
       // printf("Can't open font file!");
       perror("can open file");
    }
    /*计算偏移量*/
    int hv = *hv_lv - 0xa1;
    int lv = *(hv_lv+1) - 0xa1;
    long offset = (long) (94 * hv + lv) * 32;
    /*读取数据*/
    fseek(HZK, offset, SEEK_SET);
    fread(read_buff, 32, 1, HZK);
    fclose(HZK);

    /*打印读出数据*/
    Printf_data(read_buff);
  
}



// unsigned char read_buff[32] = {0};
// unsigned char trans_buff[32] = {0};

// int main()
// {
//     // get_one_font_16x16_from_file( "../../../../HZK16",\
//     //                         0XB0, \
//     //                         0XB2, \
//     //                         read_buff);

//     // data_trans(read_buff, trans_buff);

//     // PrintFont_ArryForamt( 0XB0,\
//     //                       0XB2,\
//     //                       trans_buff);

//    get_one_font_16x16_from_file( "../../../../HZK16",\
//                             0XB0, \
//                             0XA1, \
//                             read_buff);
//     return 0;
// }















